package com.bms.api.base.config;

import org.springframework.core.MethodParameter;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;

import com.bms.api.base.annotation.LoginMemId;
import com.bms.api.util.JwtHelper;
import com.bms.common.base.common.exception.BusinessException;
import com.bms.common.base.common.exception.EnumSvrResult;
import com.feilong.core.Validator;

@Component
public class LoginUserHandlerMethodArgumentResolver implements HandlerMethodArgumentResolver {
	

    @Override
    public boolean supportsParameter(MethodParameter parameter) {
        return parameter.getParameterName().equals("openId") && parameter.hasParameterAnnotation(LoginMemId.class);
    }

    @Override
    public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer container,
                                  NativeWebRequest request, WebDataBinderFactory factory) throws Exception {
        String token = request.getHeader("token");
        if(Validator.isNotNullOrEmpty(token)){
        	return JwtHelper.getOpenId(token);
        }
        throw new BusinessException(EnumSvrResult.ERROR_TOKEN_NULL);
    }
}
